/* Copyright 2010 by Matt Van Der Westhuizen */

package org.chaoticengine.ast.model;

import java.io.File;
import java.util.HashMap;
import java.util.Iterator;
import org.chaoticengine.ast.pattern.Model;
import org.simpleframework.xml.ElementMap;
import org.simpleframework.xml.Root;
import org.simpleframework.xml.Serializer;
import org.simpleframework.xml.core.Persister;
import org.simpleframework.xml.core.Resolve;

/**
 *
 * @author Matt Van Der Westhuizen <mattpwest@gmail.com>
 */
@Root
public class TradeInventory extends Model {
    protected HashMap<TradeGood, Float> inv = new HashMap<TradeGood, Float>();

    public TradeInventory() {
        super();
    }

    public static TradeInventory create() {
        String filename = "data/TradeInventory.xml";
        Serializer srl = new Persister();
        File source = new File(filename);
        TradeInventory ti = null;
        try {
            ti = srl.read(TradeInventory.class, source);
        } catch (Exception ex) {
            ti = new TradeInventory();
            System.out.println("Warning: Failed to load TradeInventory from file '" + filename + "'.");
            ex.printStackTrace();
        }
        return(ti);
    }

    @ElementMap(required=false, entry="Item", key="TradeGood", value="Quantity", inline=true, keyType=TradeGood.class)
    public HashMap<TradeGood, Float> getInventory() {
        return(inv);
    }

    @ElementMap(required=false, entry="Item", key="TradeGood", value="Quantity", inline=true, keyType=TradeGood.class)
    public void setInventory(HashMap<TradeGood, Float> inv) {
        this.inv = inv;
    }

    public Iterator<TradeGood> getGoods() {
        return(inv.keySet().iterator());
    }

    public float getQuantity(TradeGood good) {
        return(inv.get(good));
    }

    public void setQuantity(TradeGood good, float amount) {
        inv.put(good, amount);
    }
}
